<script setup>
import { ref, reactive, toRefs, defineProps } from "vue"
import { list, add, del, update, getDetail } from "@/page/equipment/equipment.js";
import { ElMessage, ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance();
const pageName = "生产设备"
// 按钮权限
const props = defineProps({
  idKey: {
    type: Number,
    default: null
  }
})
// 中标数据
const regisiterList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const postFormRef = ref();
const queryFormRef = ref();
const dialog = reactive({
  visible: false,
  title: ''
});

const initFormData = {
  idArray: undefined,
  productEquipmentName: '',
  productEquipmentModel: '',
  productEquipmentNumber: '',
  mainTechnicalParameter: "",
}

const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productEquipmentName: '',
    mainTechnicalParameter: '',
  },
  rules: {
    productEquipmentName: [{ required: true, message: "生产设备名称不能为空", trigger: "blur" }],
    productEquipmentModel: [{ required: true, message: "生产设备型号不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资质列表 */
const getList = async () => {
  loading.value = true;
  let parma = {
    ...queryParams.value,
    id: props.idKey
  }
  const res = await list(parma);
  regisiterList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  postFormRef.value?.resetFields();
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加设备信息";
}
/** 修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  form.value.idArray = row?.id || ids.value[0];
  const res = await getDetail(form.value.idArray);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改设备信息";
}
/** 提交按钮 */
const submitForm = () => {
  postFormRef.value?.validate(async (valid) => {
    if (valid) {
      form.value.idArray ? await update(form.value) : await add(form.value);
      dialog.visible = false;
      ElMessage({
        type: 'success',
        message: '操作成功',
      })
      await getList();
    }
  });
}
/** 删除按钮操作 */
const handleDelete = async (row) => {
  ElMessageBox.confirm(
    '确定删除吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      const idArray = row?.id || ids.value;
      del(idArray).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getList();
      })
    })
    .catch(() => {
    })

}
/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download("/system/productAchievement/export", {
    ...queryParams.value
  }, `${pageName}${new Date().getTime()}.xlsx`);
}

onMounted(() => {
  getList();
});
</script>

<template>
  <div>
    <div class="p-2">
      <div class="mb-[10px]" v-show="showSearch">
        <el-card shadow="hover">
          <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="120">
            <el-form-item label="生产设备名称" prop="productEquipmentName">
              <el-input v-model="queryParams.productEquipmentName" placeholder="请输入生产设备名称" clearable
                style="width: 200px" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="主要技术参数项" prop="mainTechnicalParameter">
              <el-input v-model="queryParams.mainTechnicalParameter" placeholder="请输入主要技术参数项" clearable
                style="width: 200px" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
      <el-card shadow="hover">
        <template #header>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">
                删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </template>

        <el-table v-loading="loading" :data="regisiterList" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="生产设备名称" align="center" prop="productEquipmentName" min-width="200"
            show-overflow-tooltip />
          <el-table-column label="生产设备型号" align="center" prop="productEquipmentModel" min-width="200" />
          <el-table-column label="数量" align="center" prop="productEquipmentNumber" min-width="200"
            show-overflow-tooltip />
          <el-table-column label="主要技术参数项" align="center" prop="mainTechnicalParameter" min-width="180" />
          <el-table-column label="操作" min-width="250" align="center" class-name="small-padding fixed-width"
            fixed="right">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-card>

      <!-- 添加或修改设备对话框 -->
      <el-dialog :title="dialog.title" v-model="dialog.visible" width="600px" append-to-body>
        <el-form ref="postFormRef" :model="form" :rules="rules" label-width="110px">
          <!-- 自动生成 -->
          <el-form-item label="生产设备名称" prop="productEquipmentName">
            <el-input v-model="form.productEquipmentName" placeholder="请输入生产设备名称" />
          </el-form-item>
          <el-form-item label="生产设备型号" prop="productEquipmentModel">
            <el-input v-model="form.productEquipmentModel" placeholder="请输入生产设备型号" />
          </el-form-item>
          <el-form-item label="数量" prop="productEquipmentNumber">
            <el-input v-model="form.productEquipmentNumber" placeholder="请输入数量" />
          </el-form-item>
          <el-form-item label="主要技术参数项" prop="mainTechnicalParameter">
            <el-input v-model="form.mainTechnicalParameter" placeholder="请输入主要技术参数项" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<style lang="scss" scoped></style>